function center_dialog(selector) {
    $(selector).dialog({position: {my: 'center', at: 'center', of: window}});
}

function center_load(selector, url, data, complete) {
    $(selector).load(
            url,
            data,
            function () {
                center_dialog(selector);
                complete();
            }
    );
}

// Accounts

function check_dialogAccount()
{
    if (!(checkClientName(document.getElementById('client_name'))))
        return false;
    if (!(document.getElementById('eai').checked))
        return true;
    if (!(checkDate(document.getElementById('datepicker_update_start_eai'), true)))
        return false;
    if (!(checkDate(document.getElementById('datepicker_update_end_eai'), true)))
        return  false;
    return true;
}

function set_dialogCreateAccount(ok, cancel) {
    $('#dialog_create_account').dialog({
        autoOpen: false, width: 1000, modal: true, draggable: false, resizable: false,
        buttons: [{
                text: ok,
                click: function () {
                    if (!check_dialogAccount())
                        return;
                    $("form[name='client_creation']").submit();
                    $(this).dialog('close');
                    load_img();
                }
            }, {
                text: cancel,
                click: function () {
                    $(this).dialog('close');
                }
            }],
        open: function () {
            center_load(
                    '#dialog_create_account',
                    $(this).data('base') + 'accounts/create',
                    undefined,
                    function () {
                        $('#datepicker_creation_start_eai').datepicker({dateFormat: 'dd/mm/yy'});
                        $('#datepicker_creation_end_eai').datepicker({dateFormat: 'dd/mm/yy'});
                    });
        }
    });
}

function set_dialogReadAccount(ok) {
    $('#dialog_read_account').dialog({
        autoOpen: false, width: 1000, modal: true,
        buttons: [
            {
                text: ok,
                click: function () {
                    $(this).dialog('close');
                }
            }],
        open: function () {
            center_load(
                    '#dialog_read_account',
                    $(this).data('base') + 'accounts/view?id=' + $(this).data('id')
                    );
        }
    });
}

function set_dialogUpdateAccount(ok, cancel) {
    $('#dialog_update_account').dialog({
        autoOpen: false, width: 1000, modal: true,
        buttons: [{
                text: ok,
                click: function () {
                    if (!check_dialogAccount())
                        return;
                    $("form[name='client_update']").submit();
                    $(this).dialog('close');
                    load_img();
                }
            },
            {
                text: cancel,
                click: function () {
                    $(this).dialog('close');
                }
            }],
        open: function () {
            center_load(
                    '#dialog_update_account',
                    $(this).data('base') + 'accounts/update?id=' + $(this).data('id'),
                    undefined,
                    function () {
//                            $('#datepicker_update_start_eai').datepicker({dateFormat: 'dd/mm/yy'});
//                            $('#datepicker_update_end_eai').datepicker({dateFormat: 'dd/mm/yy'});
                    });
        }
    });
}

function set_dialogDeleteAccount(ok, cancel) {
    $('#dialog_delete_account').dialog({
        autoOpen: false, width: 600, modal: true, draggable: false, resizable: false,
        buttons: [{
                text: ok,
                click: function () {
                    $("form[name='client_delete_" + $(this).data('id') + "']").submit();
                    $(this).dialog('close');
                    load_img();
                }
            },
            {
                text: cancel,
                click: function () {
                    $(this).dialog('close');
                }
            }]
    });
}

// Nodes

function set_dialogCreateNode(ok, cancel) {
    $('#dialog_create_node').dialog({
        autoOpen: false, width: 600, modal: true, draggable: false, resizable: false,
        buttons: [{
                text: ok,
                click: function () {
                    $("form[name='corporate_creation']").submit();
                    $(this).dialog('close');
                    load_img();
                }
            }, {
                text: cancel,
                click: function () {
                    $(this).dialog('close');
                }
            }],
        open: function () {
            center_load(
                    '#dialog_create_node',
                    $(this).data('base') + 'nodes/create'
                    );
        }
    });
}

function set_dialogUpdateNode(ok, drop, cancel) {
    $('#dialog_update_node').dialog({
        autoOpen: false, width: 600, modal: true, draggable: false, resizable: false,
        buttons: [{
                text: ok,
                click: function () {
                    $("form[name='corporate_update']").submit();
                    $(this).dialog('close');
                    load_img();
                }
            }, {
                text: drop,
                click: function () {
                    $('#dialog_delete_node').dialog('open');
                }
            }, {
                text: cancel,
                click: function () {
                    $(this).dialog('close');
                }
            }],
        open: function () {
            center_load(
                    '#dialog_update_node',
                    $(this).data('base') + 'nodes/update?id=' + $(this).data('id') + '&db=' + $(this).data('db')
                    );
        }
    });
}

function set_dialogDeleteNode(ok, cancel) {
    $('#dialog_delete_node').dialog({
        autoOpen: false, width: 600, modal: true, draggable: false, resizable: false,
        buttons: [{
                text: ok,
                click: function () {
                    $("form[name='corporate_delete']").submit();
                    $(this).dialog('close');
                    load_img();
                }
            },
            {
                text: cancel,
                click: function () {
                    $(this).dialog('close');
                }
            }]
    });
}

// Profiles

function check_dialogProfile(input_element) {
    return input_element.value.trim() !== '';
}

function set_dialogDeleteProfile(ok, cancel) {
    $('#dialog_delete_profile').dialog({
        autoOpen: false, width: 600, modal: true, draggable: false, resizable: false,
        buttons: [{
                text: ok,
                click: function () {
                    var form = $(this).data('form');
                    form.submit();
                    $(this).dialog('close');
                    load_img();
                }
            },
            {
                text: cancel,
                click: function () {
                    $(this).dialog('close');
                }
            }]
    });
}

// User

function set_dialogDeleteUser(ok, cancel) {
    $('#dialog_user_del').dialog({
        autoOpen: false, width: 600, modal: true,
        buttons: [{
                text: ok,
                click: function () {
                    $("form[name='user_delete']").submit();
                    $(this).dialog('close');
                    load_img();
                }
            },
            {
                text: cancel,
                click: function () {
                    $(this).dialog('close');
                }
            }]
    });
}